package com.ywjt.platformbusiness.store.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.order.adapter.OrderListChild_Adapter;
import com.ywjt.platformbusiness.store.order.activity.StoreOrderList;
import com.ywjt.platformbusiness.store.order.model.SortingCenterModel;
import com.ywjt.platformbusiness.store.order.model.StoreOrderListModel;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static com.ywjt.platformbusiness.common.MethodUtils.getUserId;

public class MailGoodsDetailActivity  extends BaseActivity implements View.OnClickListener {
    private static final Integer REQUESTCODE=10001;
    private  SortingCenterModel.ResultDTO model;
    private StoreOrderListModel.ResultDTO.RecordsDTO orderModel;
    private TextView tvSortName,tvDistance,tvTotalMoney,tvGoodsMoney,tvSubmit,tvReMark;
    private ImageView ivSelect;
    private LinearLayout llSortCenter;
    private EditText etGoodsName,etGoodsNum,etWeight;

    private RecyclerView rcView;
    private  OrderListChild_Adapter adapter;

    private Boolean isSelect=false;
    private String name="";//物品名称
    private String num="";//物品数量
    private String weight="";//物品重量


    public static void actionStart(Context context, SortingCenterModel.ResultDTO model, StoreOrderListModel.ResultDTO.RecordsDTO orderModel) {
        Intent intent = new Intent(context, MailGoodsDetailActivity.class);
        intent.putExtra("model",(Serializable) model);
        intent.putExtra("orderModel",(Serializable) orderModel);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case R.id.ivSelect:
                    if (!isSelect){
                        ivSelect.setImageResource(R.mipmap.icon_address_selected);
                    }else {
                        ivSelect.setImageResource(R.mipmap.icon_address_unselected);
                    }
                    isSelect=!isSelect;
                    break;
                case R.id.tvSubmit:
                    //提交
                    if (isSelect){
                        addOshopCentreMail();
                    }else {
                        Toast.makeText(MailGoodsDetailActivity.this,"您必须同意服务协议",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.llSortCenter:
                    //选择分拣中心
                    Intent intent=new Intent(MailGoodsDetailActivity.this, SortingCenterActivity.class);
                    intent.putExtra("flag", REQUESTCODE);
                    intent.putExtra("status","2");
                    startActivityForResult(intent, REQUESTCODE);
                    break;
            }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.mail_goods_detail_activity;
    }

    @Override
    protected void initView() {
        setTitleText("寄件详情");

        model=(SortingCenterModel.ResultDTO)getIntent().getSerializableExtra("model");
        orderModel=(StoreOrderListModel.ResultDTO.RecordsDTO)getIntent().getSerializableExtra("orderModel");
        tvSortName=findViewById(R.id.tvSortName);
        ivSelect=findViewById(R.id.ivSelect);
        tvTotalMoney=findViewById(R.id.tvTotalMoney);
        llSortCenter=findViewById(R.id.llSortCenter);
        tvSubmit=findViewById(R.id.tvSubmit);
        rcView=findViewById(R.id.rcView);
        tvReMark=findViewById(R.id.tvReMark);
        etGoodsNum=findViewById(R.id.etGoodsNum);
        etWeight=findViewById(R.id.etWeight);
        etGoodsName=findViewById(R.id.etGoodsName);

        tvGoodsMoney=findViewById(R.id.tvGoodsMoney);
        tvDistance=findViewById(R.id.tvDistance);
        etGoodsName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                name=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etGoodsNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                num=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etWeight.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                weight=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        ivSelect.setOnClickListener(this::onClick);
        tvSubmit.setOnClickListener(this::onClick);
        llSortCenter.setOnClickListener(this::onClick);

        if (model!=null){
            tvSortName.setText(model.getName());
            tvDistance.setText(model.getDistance()+"|"+model.getAddress());
        }

        if (orderModel!=null){
            adapter=new OrderListChild_Adapter(orderModel.getOrderDetailList(),1);
            rcView.setLayoutManager(new LinearLayoutManager(MailGoodsDetailActivity.this));
            rcView.setAdapter(adapter);

            tvGoodsMoney.setText(orderModel.getTotalMoney()+"");
            tvTotalMoney.setText("¥"+orderModel.getActuallyMoney());
            tvReMark.setText("备注："+orderModel.getRemark());
        }
    }

    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }

    //添加地址
    public void addOshopCentreMail(){
        JSONObject map = new JSONObject();


        if (name.equals("")){
            Toast.makeText(MailGoodsDetailActivity.this,"请输入物品名称",Toast.LENGTH_SHORT).show();
            return;
        }
        if (num.equals("")){
            Toast.makeText(MailGoodsDetailActivity.this,"请输入物品数量",Toast.LENGTH_SHORT).show();
            return;
        }
        if (weight.equals("")){
            Toast.makeText(MailGoodsDetailActivity.this,"请输入物品重量",Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            map.put("orderId", orderModel.getOrderNumber());
            map.put("stationId",model.getId());
            map.put("name",name);
            map.put("num",num);
            map.put("senderId",getUserId(MailGoodsDetailActivity.this));
            map.put("weight",weight);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new HttpRequest(getContext()).doPost(UrlConstants.merchant_mailDetailSubmit, "", map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {

                String data = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        StoreOrderList.actionStart(MailGoodsDetailActivity.this);
                    }else {
                        Toast.makeText(MailGoodsDetailActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK){
            if (requestCode==REQUESTCODE){
                model=(SortingCenterModel.ResultDTO) data.getSerializableExtra("sortCenter");
                if (model!=null){
                    tvSortName.setText(model.getName());
                    tvDistance.setText(model.getDistance()+"|"+model.getAddress());
                }
            }
        }
    }
}
